package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.domain.DmcDeviceAlert;
import com.csun.cmny.provider.model.dto.DeviceAlertDto;
import com.csun.cmny.provider.model.dto.follow.*;
import com.csun.cmny.provider.model.enums.device.DmcDeviceAlertTypeEnum;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.model.vo.OldmanBedRoomBuildingVo;
import com.csun.cmny.provider.model.vo.cartogram.*;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;


/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-01-16 15:57
 * @Description: the class follow data controller
 */
@RestController
@RequestMapping(value = "/follow", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcFollowDataController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private DmcFollowDataBoxygenService followDataBoxygenService;

    @Resource
    private DmcFollowDataBpressureService followDataBpressureService;

    @Resource
    private DmcFollowDataBsugarService followDataBsugarService;

    @Resource
    private DmcFollowDataEcgService followDataEcgService;

    @Resource
    private DmcFollowDataHeartService followDataHeartService;

    @Resource
    private DmcFollowDataTemperatureService followDataTemperatureService;

    @Autowired
    private DmcFollowDataBoxygenService dmcFollowDataBoxygenService;
    @Autowired
    private DmcFollowDataBpressureService dmcFollowDataBpressureService;
    @Autowired
    private DmcFollowDataBsugarService dmcFollowDataBsugarService;
    @Autowired
    private DmcFollowDataEcgService dmcFollowDataEcgService;
    @Autowired
    private DmcFollowDataHeartService dmcFollowDataHeartService;
    @Autowired
    private DmcFollowDataTemperatureService dmcFollowDataTemperatureService;
    @Autowired
    private DmcCsunButtonLocationService dmcCsunButtonLocationService;
    @Autowired
    private DmcCsunSleepBindService dmcCsunSleepBindService;
    @Autowired
    private DmcWatchDataService dmcWatchDataService;
    @Autowired
    private DmcCsunBodySensorLocationService dmcCsunBodySensorLocationService;
    @Autowired
    private DmcDeviceService dmcDeviceService;
    @Autowired
    private DmcDeviceAlertService dmcDeviceAlertService;
    /**
     * 上传随访仪血氧数据
     *
     * @param upFollowDataBoxygenDto    up follow data boxygen dto
     *
     * @return  the wrapper.
     */
    @ValidateAnnotation
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/upFollowDataBoxygen", method = RequestMethod.POST)
    public Wrapper upFollowDataBoxygen(@Validated @RequestBody UpFollowDataBoxygenDto upFollowDataBoxygenDto,
                                       BindingResult br) {

        Integer result = followDataBoxygenService.upFollowDateBoxygen(upFollowDataBoxygenDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 上传随访仪血压数据
     *
     * @param upFollowDataBpressureDto  up follow data bpressure dto
     *
     * @return  the wrapper.
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/upFollowDataBpressure", method = RequestMethod.POST)
    public Wrapper upFollowDataBpressure(@Validated @RequestBody UpFollowDataBpressureDto upFollowDataBpressureDto,
                                         BindingResult br) {

        Integer result = followDataBpressureService.upFollowDataBpressure(upFollowDataBpressureDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

      //  return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 上传随访仪血糖数据
     *
     * @param upFollowDataBsugarDto up follow data bsugar dto
     *
     * @return  the wrapper.
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/upFollowDataBsugar", method = RequestMethod.POST)
    public Wrapper upFollowDataBsugar(@Validated @RequestBody UpFollowDataBsugarDto upFollowDataBsugarDto,
                                      BindingResult br) {

        Integer result = followDataBsugarService.upFollowDataBsugar(upFollowDataBsugarDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 上传随访仪心电数据
     *
     * @param upFollowDataEcgDto    up follow data bsugar dto
     *
     * @return  the wrapper.
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/upFollowDataEcg", method = RequestMethod.POST)
    public Wrapper upFollowDataEcg(@Validated @RequestBody UpFollowDataEcgDto upFollowDataEcgDto,
                                   BindingResult br) {

        Integer result = followDataEcgService.upFollowDataEcg(upFollowDataEcgDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 上传随访仪心率数据
     *
     * @param upFollowDataHeartDto  up follow data heart dto
     *
     * @return  the wrapper.
     */
    @ValidateAnnotation
  //  @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/upFollowDataHeart", method = RequestMethod.POST)
    public Wrapper upFollowDataHeart(@Validated @RequestBody UpFollowDataHeartDto upFollowDataHeartDto,
                                     BindingResult br) {

        Integer result = followDataHeartService.upFollowDataHeart(upFollowDataHeartDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 上传随访仪体温数据
     *
     * @param upFollowDataTemperatureDto    up follow data temperature dto
     *
     * @return  the wrapper.
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/upFollowDataTemperature", method = RequestMethod.POST)
    public Wrapper upFollowDataTemperature(@Validated @RequestBody UpFollowDataTemperatureDto
                                                       upFollowDataTemperatureDto, BindingResult br) {

        if(upFollowDataTemperatureDto.getTemperature()>upFollowDataTemperatureDto.getBodyTemperature()&&PublicUtil.isNotEmpty(upFollowDataTemperatureDto.getNursingGroupId())){
            DmcDeviceAlert deviceAlert = new DmcDeviceAlert();
            Object params[] = {upFollowDataTemperatureDto.getOldmanName()};
            deviceAlert.setAlertInfo(cmnyMessageSource.getMessage("alert.message.follow.temperature",params));
            deviceAlert.setAlertType(DmcDeviceAlertTypeEnum.CSUN_FOLLOW_TEMPERATURE.getKey());
            deviceAlert.setDeviceId(upFollowDataTemperatureDto.getDeviceId());
            DmcDevice dmcDevice = dmcDeviceService.selectByKey(upFollowDataTemperatureDto.getDeviceId());

            if(PublicUtil.isNotEmpty(dmcDevice)){
                deviceAlert.setDeviceNo(dmcDevice.getDeviceNo());
            }
            deviceAlert.setGroupId(getLoginAuthDto().getGroupId());
            deviceAlert.setOldmanId(upFollowDataTemperatureDto.getOldmanId());
            deviceAlert.setOldmanName(upFollowDataTemperatureDto.getOldmanName());
            if(PublicUtil.isNotEmpty(upFollowDataTemperatureDto.getLocation())){
                deviceAlert.setLocation(upFollowDataTemperatureDto.getLocation());
            }
            deviceAlert.setNursingGroupId(upFollowDataTemperatureDto.getNursingGroupId());

            int save = dmcDeviceAlertService.save(deviceAlert);

            OldmanBedRoomBuildingVo oldmanBedRoomBuildingVo = new OldmanBedRoomBuildingVo();
            dmcDeviceAlertService.sendDeviceAlertMsg(deviceAlert,oldmanBedRoomBuildingVo);


        }
        Integer result = followDataTemperatureService
                .upFollowDataTemperature(upFollowDataTemperatureDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * query dmc follow data boxygen list with page
     *
     * @param followDataPageQuery the follow data query page
     *
     * @return the wrapper
     */
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/queryDmcFollowDataBoxygenListWithPage", method = RequestMethod.POST)
    public Wrapper queryDmcFollowDataBoxygenListWithPage(@RequestBody FollowDataPageQuery followDataPageQuery) {

        PageInfo pageInfo = followDataBoxygenService.queryDmcFollowDataBoxygenListWithPage(followDataPageQuery);

       // return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    /**
     * query dmc follow data bpressure list with page
     *
     * @param followDataPageQuery the follow data query page
     *
     * @return the wrapper
     */
   // @LogAnnotation
    @RequestMapping(value = "/queryDmcFollowDataBpressureListWithPage", method = RequestMethod.POST)
    public Wrapper queryDmcFollowDataBpressureListWithPage(@RequestBody FollowDataPageQuery followDataPageQuery) {

        PageInfo pageInfo = followDataBpressureService.queryDmcFollowDataBpressureListWithPage(followDataPageQuery);

       // return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    /**
     * query dmc follow data bsugar list with page
     *
     * @param followDataPageQuery the follow data query page
     *
     * @return the wrapper
     */
   // @LogAnnotation
    @RequestMapping(value = "/queryDmcFollowDataBsugarListWithPage", method = RequestMethod.POST)
    public Wrapper queryDmcFollowDataBsugarListWithPage(@RequestBody FollowDataPageQuery followDataPageQuery) {

        PageInfo pageInfo = followDataBsugarService.queryDmcFollowDataBsugarListWithPage(followDataPageQuery);

       // return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    /**
     * query dmc follow data ecg list with page
     *
     * @param followDataPageQuery the follow data query page
     *
     * @return the wrapper
     */
   // @LogAnnotation
    @RequestMapping(value = "/queryDmcFollowDataEcgListWithPage", method = RequestMethod.POST)
    public Wrapper queryDmcFollowDataEcgListWithPage(@RequestBody FollowDataPageQuery followDataPageQuery) {

        PageInfo pageInfo = followDataEcgService.queryDmcFollowDataEcgListWithPage(followDataPageQuery);

       // return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    /**
     * query dmc follow data heart list with page
     *
     * @param followDataPageQuery the follow data query page
     *
     * @return the wrapper
     */
   /// @LogAnnotation
    @RequestMapping(value = "/queryDmcFollowDataHeartListWithPage", method = RequestMethod.POST)
    public Wrapper queryDmcFollowDataHeartListWithPage(@RequestBody FollowDataPageQuery followDataPageQuery) {

        PageInfo pageInfo = followDataHeartService.queryDmcFollowDataHeartListWithPage(followDataPageQuery);

      //  return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    /**
     * query dmc follow data temperature list with page
     *
     * @param followDataPageQuery the follow data query page
     *
     * @return the wrapper
     */
   // @LogAnnotation
    @RequestMapping(value = "/queryDmcFollowDataTemperatureListWithPage", method = RequestMethod.POST)
    public Wrapper queryDmcFollowDataTemperatureListWithPage(@RequestBody FollowDataPageQuery followDataPageQuery) {

        PageInfo pageInfo = followDataTemperatureService.queryDmcFollowDataTemperatureListWithPage(followDataPageQuery);

       // return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    @PostMapping

    @RequestMapping(value = "/getoldManDevices",method = RequestMethod.POST)
    public Wrapper getOldMnaDevices(@RequestParam("oldmanId")Integer oldmanId){
        DmcOldManDevice sfy = new DmcOldManDevice();
        //随访仪数据
        sfy.setFollowName("随访仪");
        DmcFollowVo dmcFollowVo = new DmcFollowVo();
        //1.血氧测量值
        Map<String, Object> bOxygen = dmcFollowDataBoxygenService.getRecentData(oldmanId);
        if(bOxygen!=null){
            dmcFollowVo.setBoxygen((Integer) bOxygen.get("boxygen"));
            dmcFollowVo.setBoxygenStatus((String)bOxygen.get("status"));
            dmcFollowVo.setHasDevice(1);
        }
        //2.血压测量值
        Map<String, Object> bPressure = dmcFollowDataBpressureService.getRecentData(oldmanId);
        if(bPressure!=null){
            dmcFollowVo.setSbp((Integer)bPressure.get("sbp"));
            dmcFollowVo.setDbp((Integer)bPressure.get("dbp"));
            dmcFollowVo.setBPressureStatus((String)bPressure.get("status"));
            dmcFollowVo.setHasDevice(1);
        }
        //3.血糖测量值
        Map<String, Object> bSugar = dmcFollowDataBsugarService.getRecentData(oldmanId);
        if(bSugar!=null){
            dmcFollowVo.setBSugar((Float)bSugar.get("bsugar"));
            dmcFollowVo.setBSugarStatus((String)bSugar.get("status"));
            dmcFollowVo.setHasDevice(1);
        }
        //4.心电测量值
        Map<String, Object> ecg = dmcFollowDataEcgService.getRecentData(oldmanId);
        if(ecg!=null){
            dmcFollowVo.setPrMin((String)ecg.get("pr_min"));
            dmcFollowVo.setPrMax((String)ecg.get("pr_max"));
            dmcFollowVo.setMood((String)ecg.get("mood"));
            dmcFollowVo.setGain((String)ecg.get("gain"));
            dmcFollowVo.setHrv((String)ecg.get("hrv"));
            dmcFollowVo.setEcg((String)ecg.get("ecg"));
            dmcFollowVo.setHasDevice(1);
        }
        //5.心率测量值
        Map<String, Object> heart = dmcFollowDataHeartService.getRecentData(oldmanId);
        if(heart!=null){
            dmcFollowVo.setHeartRate((Integer)heart.get("heart_rate"));
            dmcFollowVo.setHeartStatus((String)heart.get("status"));
            dmcFollowVo.setHasDevice(1);
        }
        //6.体温测量值
        Map<String, Object> temperature = dmcFollowDataTemperatureService.getRecentData(oldmanId);
        if(temperature!=null){
            dmcFollowVo.setTemperature((Float)temperature.get("temperature"));
            dmcFollowVo.setTemperatureStatsu((String)temperature.get("status"));
            dmcFollowVo.setHasDevice(1);
        }

        sfy.setFollowObject(dmcFollowVo);

        //紧急按钮
        DmcAlertVo dmcAlert = new DmcAlertVo();
        sfy.setButtonName("紧急按钮");
        Map<String, Object> buttonLocation = dmcCsunButtonLocationService.getRecentData(oldmanId);
        if(buttonLocation!=null && buttonLocation.size()>0){
            dmcAlert.setAlertType((Integer)buttonLocation.get("alertType"));
            dmcAlert.setAlertInfo((String) buttonLocation.get("alertInfo"));
            dmcAlert.setLocation((String)buttonLocation.get("location"));
            dmcAlert.setAlertTypeName(DmcDeviceAlertTypeEnum.getValue((Integer)buttonLocation.get("alertType")));
            dmcAlert.setHasDevice(1);
        }
        if(dmcAlert!=null){sfy.setButtonObject(dmcAlert);}

        //日创床垫
        DmcBedVo dmcBedVo = new DmcBedVo();
        sfy.setBedName("日创床垫");
        Map<String, Object> sleep = dmcCsunSleepBindService.getRecentData(oldmanId);
        if(sleep!=null){
            dmcBedVo.setBreath((Integer)sleep.get("breath"));
            dmcBedVo.setHeartBeat((Integer)sleep.get("heartbeat"));
            dmcBedVo.setDeviceNo((String)sleep.get("deviceNo"));
            dmcBedVo.setHasDevice(1);
            dmcBedVo.setDeviceId((Long)sleep.get("deviceId"));
        }
        sfy.setBedObject(dmcBedVo);

        //孝心手环
        DmcWatchVo dmcWatchVo = new DmcWatchVo();
        sfy.setWatchName("孝心手环");
        Map<String, Object> watch = dmcWatchDataService.getRecentData(oldmanId);
        if(watch!=null){
            dmcWatchVo.setPower((Integer)watch.get("power"));
            dmcWatchVo.setStep((Integer)watch.get("step"));
            dmcWatchVo.setHeartRate((Integer)watch.get("heartRate"));
            dmcWatchVo.setLocation((String)watch.get("location"));
            dmcWatchVo.setDeviceNo((String)watch.get("deviceNo"));
            dmcWatchVo.setHasDevice(1);
            dmcWatchVo.setDeviceId((Long)watch.get("deviceId"));
        }
        sfy.setWatchObject(dmcWatchVo);

        //人体感应
        sfy.setBodyName("人体感应");
        DmcAlertVo dmcAlertVo = new DmcAlertVo();
        Map<String, Object> bodySensor = dmcCsunBodySensorLocationService.getRecentData(oldmanId);
        if(bodySensor!=null && bodySensor.size()>0){
            dmcAlertVo.setAlertType((Integer)bodySensor.get("alertType"));
            dmcAlertVo.setAlertInfo((String) bodySensor.get("alertInfo"));
            dmcAlertVo.setLocation((String)bodySensor.get("location"));
            dmcAlertVo.setAlertTypeName(DmcDeviceAlertTypeEnum.getValue((Integer)bodySensor.get("alertType")));
            dmcAlertVo.setHasDevice(1);
        }
        if(dmcAlertVo!=null){sfy.setBodyObject(dmcAlertVo);}

      //  return WrapMapper.ok(sfy);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),sfy);
    }

    @RequestMapping(value = "/getHistoryData",method = RequestMethod.POST)
    public Wrapper getHistoryData(@RequestParam("oldmanId")Integer oldmanId){
        DmcOldManHistory returnData = new DmcOldManHistory();
        //血压所有历史数据
        returnData.setPressureHistoryData(dmcFollowDataBpressureService.getAllOldmanData(oldmanId));
        //心电所有历史数据
        returnData.setEcgHistoryData(dmcFollowDataEcgService.getAllOldmanData(oldmanId));
        //心率所有历史数据
        returnData.setHeartHistoryData(dmcFollowDataHeartService.getAllOldmanData(oldmanId));
       // return WrapMapper.ok(returnData);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),returnData);
    }

    @RequestMapping(value = "/queryDmcFollowDataTemperatureListWithPages", method = RequestMethod.POST)
    public Wrapper queryDmcFollowDataTemperatureListWithPages(@RequestBody FollowDataPageQuery followDataPageQuery) {

        //followDataPageQuery.setGroupId(getLoginAuthDto().getGroupId());

        PageInfo pageInfo = followDataTemperatureService.queryDmcFollowDataTemperatureListWithPages(followDataPageQuery);

        // return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }


}
